﻿using ManaMaple.Kit.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace ManaMaple.Kit.Models
{
    /// <summary>
    /// ** 描述：操作返回
    /// ** 时间：2022-7-7
    /// ** 作者：杨逢春
    /// </summary>
    public class OperateResult
    {

        #region 属性
        /// <summary>
        /// 成功
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 返回结果内容 成功：Success  失败：异常内容
        /// </summary>
        public string ResultMsg { get; set; } = string.Empty;
        /// <summary>
        /// 返回的可选信息
        /// </summary>
        public object? ReturnData { get; set; } = null;
        #endregion

        #region 构造
        /// <summary>
        /// 构造函数 
        /// </summary>
        public OperateResult()
        {
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="success">成功还是失败</param>
        public OperateResult(bool success)
            : this()
        {
            this.Success = success;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="success">成功还是失败</param>
        /// <param name="resultMsg">返回结果内容 成功：Success  失败：异常内容</param>
        public OperateResult(bool success, string resultMsg)
            : this(success)
        {
            this.ResultMsg = resultMsg;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="success">成功还是失败</param>
        /// <param name="resultMsg">信息</param>
        /// <param name="returnData">强制返回值</param>
        public OperateResult(bool success, string resultMsg, object returnData)
            : this(success, resultMsg)
        {
            this.ReturnData = returnData;
        }
        #endregion

        #region 方法

        /// <summary>
        /// 构建操作成功信息
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public void GetSuccess(object data, string message = "")
        {
            this.ReturnData = data;
            this.ResultMsg = message;
            this.Success = true;
        }

        /// <summary>
        /// 构建操作成功信息
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public void GetFail(string msg)
        {
            this.ResultMsg = msg;
            this.Success = false;
        }

        /// <summary>
        /// 构建操作成功信息
        /// </summary>
        /// <param name="returnData"></param>
        /// <returns></returns>
        public static OperateResult SuccessOperate(object returnData = null)
        {
            return new OperateResult(true, "Success", returnData);
        }
        /// <summary>
        /// 构建操作失败信息
        /// </summary>
        /// <param name="resultMsg"></param>
        /// <param name="returnData"></param>
        /// <returns></returns>
        public static OperateResult FailedOperate(string resultMsg, object returnData = null)
        {
            return new OperateResult(false, resultMsg, returnData);
        }
        #endregion
    }
}
